package cn.gson.springboot.controller.presalecontroller;

import cn.gson.springboot.model.pjos.*;
import cn.gson.springboot.model.service.presaleservice.PsBusinessDetailsService;
import cn.gson.springboot.model.vo.presalevo.HistoryProducts;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商机详情mapper
 */
@Controller
@RequestMapping("/buDetails")
public class PsBusinessDetailsController {
    @Autowired
    PsBusinessDetailsService  businessDetailsService;

    //查询所有的跟进类型
    @RequestMapping("/follow_type")
    @ResponseBody
    public List<FollowType> allFollowType(){
        return businessDetailsService.allFollowType();
    }

    //根据跟进类型查询出对应的快捷添加语言
    @RequestMapping("/follow_quick")
    @ResponseBody
    public List<Quick> quickAdd(int follId){
        return businessDetailsService.quickAdd(follId);
    }

    //新增商机的跟进记录
    @RequestMapping("/follow_up")
    @ResponseBody
    public int save(String followUp){
        System.err.println(">>>>>>>>"+followUp);
        //将字符串转化成JSONObject
        JSONObject obj=(JSONObject) JSONObject.parse(followUp);
        FollowUp follow=obj.getObject("follow",FollowUp.class);
        return  businessDetailsService.saveFollow(follow);
    }

    //新增快捷模板
    @RequestMapping("/quick")
    @ResponseBody
    public Integer quick(@RequestBody Quick quick){
           return businessDetailsService.savaQuick(quick);
    }
    /*查询该商机的跟进记录*/
    @RequestMapping("/select_followUp")
    @ResponseBody
    public List<FollowUp> selectFollowUp(Integer businesId){
        return businessDetailsService.selectFollowUp(businesId);
    }

    /*查询对应跟进类型的快捷信息*/
    @RequestMapping("/all_quicks")
    @ResponseBody
    public List<Quick> allQuicks(){
        return businessDetailsService.allQuicks();
    }

    /*删除快捷模板*/
    @RequestMapping("/delete_quicks")
    @ResponseBody
    public void deleteQuick(Integer quickId){
        businessDetailsService.deleteQuick(quickId);
    }

    /**
     * save 详细需求
     * @param demand
     */
    @RequestMapping("save_demand")
    @ResponseBody
    public void saveDemand(@RequestBody Demand demand){
        businessDetailsService.saveDemand(demand);
    }

    /**
     * 查询商机下的详细需求
     * @param businesId
     * @return
     */
    @RequestMapping("/query_demand")
    @ResponseBody
    public List<Demand> demandList(Integer businesId){
        return  businessDetailsService.queryBusinessDemand(businesId);
    }

    @RequestMapping("/save_competitor")
    @ResponseBody
    public void saveCompetitor(String competitor){
        System.err.println(competitor);
        JSONObject obj=(JSONObject) JSONObject.parse(competitor);
        Competitor competitor1=obj.getObject("competitor",Competitor.class);
        businessDetailsService.saveCompetitor(competitor1);
    }

    //删除竞争对手
    @RequestMapping("/delete_competitor")
    @ResponseBody
    public void deleteCompetitor(Integer competitorId){
        businessDetailsService.deleteCompetitor(competitorId);
    }

    /**查询商机的竞争对手*/
    @RequestMapping("/select_competitor")
    @ResponseBody
    public List<Competitor> competitorList(int businesId){
       return businessDetailsService.competitorList(businesId);
    }

    /**方案的新增或修改*/
    @RequestMapping("/save_programme")
    @ResponseBody
    public int saveProgramme(String programme){
        JSONObject obj=(JSONObject) JSONObject.parse(programme);
        Programme programme1=obj.getObject("programme",Programme.class);
        return  businessDetailsService.saveProgramme(programme1);
    }

    /**查询商机的解决方案*/
    @RequestMapping("/select_programme")
    @ResponseBody
    public List<Programme> programmeList(int businesId){
        return businessDetailsService.programmeList(businesId);
    }
    /**查询商机下的客户联系人*/
    @RequestMapping("/select_culinkman")
    @ResponseBody
    public List<BusinessInfo> select_culinkman(int businesId){
        return businessDetailsService.select_culinkman(businesId);
    }

    /*查询客户所有的联系人以及联系人的模糊查询*/
    @RequestMapping("/culinkmanList")
    @ResponseBody
    public List<Culinkman> culinkmanList(Integer customerId,String cupName){
        return businessDetailsService.culinkmanList(customerId,cupName);
    }

    /*修改商机的绑定的客户联系人*/
    @RequestMapping("/updateCulinkman")
    @ResponseBody
    public void update(String cutact){
        JSONObject json=(JSONObject)JSONObject.parse(cutact);
        Integer businesId=json.getInteger("businesId");
        Integer cupId=json.getInteger("cupId");
        businessDetailsService.updateCulinkman(businesId,cupId);
    }

    /**
     * save 竞品
     * @param competingProducts
     */
    @RequestMapping("/save_comProducts")
    @ResponseBody
    public void saveComProducts(@RequestBody CompetingProducts competingProducts){
        businessDetailsService.saveComProducts(competingProducts);
    }

    /**
     * 查询竞品
     * @param comProPageSize
     * @param comProPageNum
     * @param comProductsName
     * @return
     */
    @RequestMapping("/select_comProducts")
    @ResponseBody
    public Map<String,Object> comProducts(Integer comProPageSize ,Integer comProPageNum,String comProductsName){
        System.out.println("aa:"+comProductsName);
        Page<Object> obj = PageHelper.startPage(comProPageSize,comProPageNum);
        businessDetailsService.allComProducts(comProductsName);
        Map<String,Object> map = new HashMap<>();
        map.put("comProductsList",obj.getResult());
        map.put("comProductsTotal",obj.getTotal());
        return  map;
    }

    /**
     * 查询竞品名称是否出现重复
     * @param comProductName
     * @return
     */
    @RequestMapping("/is_not_comProductsName")
    @ResponseBody
    public int isNotComProductsName(String comProductName){
        return businessDetailsService.isNotComProductsName(comProductName);
    }

    /**
     * 查询出所有的竞争公司
     * @return
     */
    @RequestMapping("/select_all_competition")
    @ResponseBody
    public List<Competitor> selectAllCompetition(){
        return  businessDetailsService.selectAllCompetition();
    }

    /**
     * 删除详细需求
     * @param demandId
     */
    @RequestMapping("/delete_demand")
    @ResponseBody
    public void deleteDemand(Integer demandId){
        businessDetailsService.deleteDemand(demandId);
    }

    /**
     * 查看客户的历史产品
     * @param prName
     * @return
     */
    @RequestMapping("/select_histroy_products")
    @ResponseBody
    public List<HistoryProducts> customerHistoryProducts(Integer customId,String prName){
        return businessDetailsService.customerHistoryProducts(customId,prName);
    }

}
